﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using FrienDev.Data;
using FrienDev.BusinessRules;

public partial class Controls_Users_Profile : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        User user = null;
        if (ApiHelper.Instance.IsFriendsPage())
        {
            user = UserRules.Instance.GetUserByUserId(ApiHelper.Instance.GetRequestUserId());
            InitItems_FriendPage();
            img_UserLogo.ImageUrl = ApiHelper.Instance.GetUserBigLogoUrl(user.UserId);
            if (!ApiHelper.Instance.IsVisible(UserRules.Instance.GetCurrentUserId(), ApiHelper.Instance.GetRequestUserId(), "DefaultPage"))
            {
                InitUserBasicInfo(user);
                UnVisibleUserDetailInfo();
                pn_UserContactInfoTitle.Visible = false;
                UnVisibleUserContactlInfo();
                pn_Birthday.Visible = false;
                pn_Email.Visible = false;
                pn_FavoriteTechnical.Visible = false;
                pn_Description.Visible = false;
                pn_WorkPlace.Visible = false;
                pn_Privacy.Visible = true;
            }
            else
            {
                if (!UserRules.Instance.UserDetailInfoExisted(user.UserId.ToString()))
                {
                    InitUserDetailInfo(user);
                }
                else
                {
                    UnVisibleUserDetailInfo();
                }
                if (!UserRules.Instance.UserContactInfoExisted(user.UserId.ToString()))
                {
                    InitUserContactInfo(user);
                }
                else
                {
                    UnVisibleUserContactlInfo();
                }
                InitUserBasicInfo(user);
            }
        }
        else
        {
            user = UserRules.Instance.GetCurrentUser();
            InitItems_SelfPage();
            img_UserLogo.ImageUrl = ApiHelper.Instance.GetUserBigLogoUrl(user.UserId);
            if (!UserRules.Instance.UserDetailInfoExisted(user.UserId.ToString()))
            {
                InitUserDetailInfo(user);
            }
            else
            {
                UnVisibleUserDetailInfo();
            }
            if (!UserRules.Instance.UserContactInfoExisted(user.UserId.ToString()))
            {
                InitUserContactInfo(user);
            }
            else
            {
                UnVisibleUserContactlInfo();
            }
            InitUserBasicInfo(user);
        }
        btn_AddFriend.Click += new EventHandler(btn_AddFriend_Click);
    }

    private void btn_AddFriend_Click(object sender, EventArgs e)
    {
        FriendRules.Instance.AddFriend(UserRules.Instance.GetCurrentUserId(), ApiHelper.Instance.GetRequestUserId(),
            txtRequestText.Text.Trim());
        ApiHelper.Instance.SendSystemMessage("Friend",
            "Request",
            ApiHelper.Instance.GetRequestUserId(),
            new string[] { UserRules.Instance.GetCurrentUserId(), UserRules.Instance.GetCurrentUser().FullName, 
                txtRequestText.Text.Trim()});
    }


    #region 初始化用户信息
    private void InitUserBasicInfo(User user)
    {
        lb_Name.Text = user.FullName.ToString();
        lb_Gender.Text = GetGenderName(user.Gender);
        if (ApiHelper.Instance.IsFriendsPage())
        {
            if (ApiHelper.Instance.IsVisible(UserRules.Instance.GetCurrentUserId(), ApiHelper.Instance.GetRequestUserId(), "ProfileBasicInfo"))
            {
                lb_Birthday.Text = Convert.ToDateTime(user.Birthday.ToString()).ToString("yyyy年MM月dd日");
                if (string.IsNullOrEmpty(user.CityId.ToString()))
                {
                    pn_WorkPlace.Visible = false;
                }
                else
                {
                    lb_WorkPlace.Text = UserRules.Instance.GetProvinceByCityId(Convert.ToInt32(user.CityId.ToString())).ProvinceName.ToString() + "-" + user.City.CityName.ToString();
                }
                lb_Email.Text = user.Email.ToString();
                if (string.IsNullOrEmpty(user.FavoriteTechnical.ToString()))
                {
                    pn_FavoriteTechnical.Visible = false;
                }
                else
                {
                    string favoritetechnical = "";
                    pn_UserDetailInfoTitle.Visible = true;
                    string[] tArray = user.FavoriteTechnical.Split('-');
                    if (tArray[0] != "")
                    {
                        foreach (string t in tArray)
                        {
                            favoritetechnical = favoritetechnical + DictionaryRules.Instance.GetTechnologyValue(Convert.ToInt32(t)) + "<br>";
                        }
                    }
                    lb_FavoriteTechnical.Text = favoritetechnical;
                }
                if (string.IsNullOrEmpty(user.Description.ToString()))
                {
                    pn_Description.Visible = false;
                }
                else
                {
                    pn_UserDetailInfoTitle.Visible = true;
                    lb_Description.Text = user.Description.ToString();
                }
            }
            else
            {
                pn_Birthday.Visible = false;
                pn_Email.Visible = false;
                pn_FavoriteTechnical.Visible = false;
                pn_Description.Visible = false;
                pn_WorkPlace.Visible = false;
            }
            if (pn_Blog.Visible == false && pn_Email.Visible == false && pn_fax.Visible == false && pn_MSN.Visible == false && pn_QQ.Visible == false && pn_Telephone.Visible == false && pn_MobilePhone.Visible == false)
            {
                pn_UserContactInfoTitle.Visible = false;
            }
        }
        else
        {
            lb_Gender.Text = GetGenderName(user.Gender);
            lb_Birthday.Text = Convert.ToDateTime(user.Birthday.ToString()).ToString("yyyy年MM月dd日");
            if (string.IsNullOrEmpty(user.CityId.ToString()))
            {
                pn_WorkPlace.Visible = false;
            }
            else
            {
                lb_WorkPlace.Text = UserRules.Instance.GetProvinceByCityId(Convert.ToInt32(user.CityId.ToString())).ProvinceName.ToString() + "-" + user.City.CityName.ToString();
            }
            lb_Email.Text = user.Email.ToString();
            if (string.IsNullOrEmpty(user.FavoriteTechnical.ToString()))
            {
                pn_FavoriteTechnical.Visible = false;
            }
            else
            {
                string favoritetechnical = "";
                pn_UserDetailInfoTitle.Visible = true;
                string[] tArray = user.FavoriteTechnical.Split('-');
                if (tArray[0] != "")
                {
                    foreach (string t in tArray)
                    {
                        favoritetechnical = favoritetechnical + DictionaryRules.Instance.GetTechnologyValue(Convert.ToInt32(t)) + "<br>";
                    }
                }
                lb_FavoriteTechnical.Text = favoritetechnical;
            }
            if (string.IsNullOrEmpty(user.Description.ToString()))
            {
                pn_Description.Visible = false;
            }
            else
            {
                pn_UserDetailInfoTitle.Visible = true;
                lb_Description.Text = user.Description.ToString();
            }
        }
    }

    /// <summary>
    /// 初始化用户详细信息
    /// </summary>
    /// <param name="user"></param>
    private void InitUserDetailInfo(User user)
    {
        if (ApiHelper.Instance.IsFriendsPage())
        {
            if (ApiHelper.Instance.IsVisible(UserRules.Instance.GetCurrentUserId(), ApiHelper.Instance.GetRequestUserId(), "ProfileDetailInfo"))
            {
                if (string.IsNullOrEmpty(user.UsersDetail.BlogAddress.ToString()))
                {
                    pn_Blog.Visible = false;
                }
                else
                {
                    lb_Blog.Text = user.UsersDetail.BlogAddress.ToString();
                }
                if (string.IsNullOrEmpty(user.UsersDetail.EduBackground.ToString()))
                {
                    pn_EduBackground.Visible = false;
                }
                else
                {
                    lb_EduBackground.Text = user.UsersDetail.Dictionary3.ObjectValue.ToString();
                }
                if (string.IsNullOrEmpty(user.UsersDetail.CompanyName.ToString()))
                {
                    pn_CompanyName.Visible = false;
                }
                else
                {
                    lb_CompanyName.Text = user.UsersDetail.CompanyName.ToString();
                }
                if (string.IsNullOrEmpty(user.UsersDetail.CompanyTypeId.ToString()))
                {
                    pn_CompanyType.Visible = false;
                }
                else
                {
                    lb_CompanyType.Text = user.UsersDetail.Dictionary.ObjectValue.ToString();
                }
                if (string.IsNullOrEmpty(user.UsersDetail.IndustryTypeId.ToString()))
                {
                    pn_IndustryType.Visible = false;
                }
                else
                {
                    lb_IndustryType.Text = user.UsersDetail.Dictionary1.ObjectValue.ToString();
                }
                if (string.IsNullOrEmpty(user.UsersDetail.CompanyScale.ToString()))
                {
                    pn_CompanyScale.Visible = false;
                }
                else
                {
                    lb_CompanyScale.Text = user.UsersDetail.Dictionary5.ObjectValue.ToString();
                }
                if (string.IsNullOrEmpty(user.UsersDetail.TitleId.ToString()))
                {
                    pn_Title.Visible = false;
                }
                else
                {
                    lb_Title.Text = user.UsersDetail.Dictionary2.ObjectValue.ToString();
                }
                if (string.IsNullOrEmpty(user.UsersDetail.WorkExperience.ToString()))
                {
                    pn_WorkExperience.Visible = false;
                }
                else
                {
                    lb_WorkExperience.Text = user.UsersDetail.Dictionary4.ObjectValue.ToString();
                }
                if (string.IsNullOrEmpty(user.UsersDetail.Department.ToString()))
                {
                    pn_Department.Visible = false;
                }
                else
                {
                    lb_Department.Text = user.UsersDetail.Department.ToString();
                }
                if (string.IsNullOrEmpty(user.UsersDetail.CompanyAddress.ToString()))
                {
                    pn_CompanyAddress.Visible = false;
                }
                else
                {
                    lb_CompanyAddress.Text = user.UsersDetail.CompanyAddress.ToString();
                }
                if (string.IsNullOrEmpty(user.UsersDetail.PostCode.ToString()))
                {
                    pn_PostCode.Visible = false;
                }
                else
                {
                    lb_PostCode.Text = user.UsersDetail.PostCode.ToString();
                }
            }
            else
            {
                UnVisibleUserDetailInfo();
            }
        }
        else
        {
            if (string.IsNullOrEmpty(user.UsersDetail.BlogAddress.ToString()))
            {
                pn_Blog.Visible = false;
            }
            else
            {
                lb_Blog.Text = user.UsersDetail.BlogAddress.ToString();
            }
            if (string.IsNullOrEmpty(user.UsersDetail.EduBackground.ToString()))
            {
                pn_EduBackground.Visible = false;
            }
            else
            {
                lb_EduBackground.Text = user.UsersDetail.Dictionary3.ObjectValue.ToString();
            }
            if (string.IsNullOrEmpty(user.UsersDetail.CompanyName.ToString()))
            {
                pn_CompanyName.Visible = false;
            }
            else
            {
                lb_CompanyName.Text = user.UsersDetail.CompanyName.ToString();
            }
            if (string.IsNullOrEmpty(user.UsersDetail.CompanyTypeId.ToString()))
            {
                pn_CompanyType.Visible = false;
            }
            else
            {
                lb_CompanyType.Text = user.UsersDetail.Dictionary.ObjectValue.ToString();
            }
            if (string.IsNullOrEmpty(user.UsersDetail.IndustryTypeId.ToString()))
            {
                pn_IndustryType.Visible = false;
            }
            else
            {
                lb_IndustryType.Text = user.UsersDetail.Dictionary1.ObjectValue.ToString();
            }
            if (string.IsNullOrEmpty(user.UsersDetail.CompanyScale.ToString()))
            {
                pn_CompanyScale.Visible = false;
            }
            else
            {
                lb_CompanyScale.Text = user.UsersDetail.Dictionary5.ObjectValue.ToString();
            }
            if (string.IsNullOrEmpty(user.UsersDetail.TitleId.ToString()))
            {
                pn_Title.Visible = false;
            }
            else
            {
                lb_Title.Text = user.UsersDetail.Dictionary2.ObjectValue.ToString();
            }
            if (string.IsNullOrEmpty(user.UsersDetail.WorkExperience.ToString()))
            {
                pn_WorkExperience.Visible = false;
            }
            else
            {
                lb_WorkExperience.Text = user.UsersDetail.Dictionary4.ObjectValue.ToString();
            }
            if (string.IsNullOrEmpty(user.UsersDetail.Department.ToString()))
            {
                pn_Department.Visible = false;
            }
            else
            {
                lb_Department.Text = user.UsersDetail.Department.ToString();
            }
            if (string.IsNullOrEmpty(user.UsersDetail.CompanyAddress.ToString()))
            {
                pn_CompanyAddress.Visible = false;
            }
            else
            {
                lb_CompanyAddress.Text = user.UsersDetail.CompanyAddress.ToString();
            }
            if (string.IsNullOrEmpty(user.UsersDetail.PostCode.ToString()))
            {
                pn_PostCode.Visible = false;
            }
            else
            {
                lb_PostCode.Text = user.UsersDetail.PostCode.ToString();
            }
        }
    }

    /// <summary>
    /// 不显示用户详细信息--当用户还未填写时
    /// </summary>
    private void UnVisibleUserDetailInfo()
    {
        pn_UserDetailInfoTitle.Visible = false;
        pn_Blog.Visible = false;
        pn_EduBackground.Visible = false;
        pn_CompanyName.Visible = false;
        pn_CompanyType.Visible = false;
        pn_CompanyScale.Visible = false;
        pn_IndustryType.Visible = false;
        pn_Title.Visible = false;
        pn_WorkExperience.Visible = false;
        pn_Department.Visible = false;
        pn_CompanyAddress.Visible = false;
        pn_PostCode.Visible = false;
    }

    /// <summary>
    /// 初始化用户联系方式信息
    /// </summary>
    /// <param name="user"></param>
    private void InitUserContactInfo(User user)
    {
        if (ApiHelper.Instance.IsFriendsPage())
        {
            if (ApiHelper.Instance.IsVisible(UserRules.Instance.GetCurrentUserId(), ApiHelper.Instance.GetRequestUserId(), "QQ"))
            {
                if (string.IsNullOrEmpty(user.UsersContact.QQ.ToString()))
                {
                    pn_QQ.Visible = false;
                }
                else
                {
                    lb_QQ.Text = user.UsersContact.QQ.ToString();
                }
            }
            else
            {
                pn_QQ.Visible = false;
            }
            if (ApiHelper.Instance.IsVisible(UserRules.Instance.GetCurrentUserId(), ApiHelper.Instance.GetRequestUserId(), "MSN"))
            {
                if (string.IsNullOrEmpty(user.UsersContact.MSN.ToString()))
                {
                    pn_MSN.Visible = false;
                }
                else
                {
                    lb_MSN.Text = user.UsersContact.MSN.ToString();
                }
            }
            else
            {
                pn_MSN.Visible = false;
            }
            if (ApiHelper.Instance.IsVisible(UserRules.Instance.GetCurrentUserId(), ApiHelper.Instance.GetRequestUserId(), "Telephone"))
            {
                if (string.IsNullOrEmpty(user.UsersContact.Phone.ToString()))
                {
                    pn_Telephone.Visible = false;
                }
                else
                {
                    lb_Telephone.Text = user.UsersContact.Phone.ToString();
                }
            }
            else
            {
                pn_Telephone.Visible = false;
            }
            if (ApiHelper.Instance.IsVisible(UserRules.Instance.GetCurrentUserId(), ApiHelper.Instance.GetRequestUserId(), "MobilePhone"))
            {
                if (string.IsNullOrEmpty(user.UsersContact.MobilePhone.ToString()))
                {
                    pn_MobilePhone.Visible = false;
                }
                else
                {
                    lb_MobilePhone.Text = user.UsersContact.MobilePhone.ToString();
                }
            }
            else
            {
                pn_MobilePhone.Visible = false;
            }
            if (ApiHelper.Instance.IsVisible(UserRules.Instance.GetCurrentUserId(), ApiHelper.Instance.GetRequestUserId(), "Fax"))
            {
                if (string.IsNullOrEmpty(user.UsersContact.Fax.ToString()))
                {
                    pn_fax.Visible = false;
                }
                else
                {
                    lb_fax.Text = user.UsersContact.Fax.ToString();
                }
            }
            else
            {
                pn_fax.Visible = false;
            }
        }
        else
        {
            if (string.IsNullOrEmpty(user.UsersContact.QQ.ToString()))
            {
                pn_QQ.Visible = false;
            }
            else
            {
                lb_QQ.Text = user.UsersContact.QQ.ToString();
            }
            if (string.IsNullOrEmpty(user.UsersContact.MSN.ToString()))
            {
                pn_MSN.Visible = false;
            }
            else
            {
                lb_MSN.Text = user.UsersContact.MSN.ToString();
            }
            if (string.IsNullOrEmpty(user.UsersContact.Phone.ToString()))
            {
                pn_Telephone.Visible = false;
            }
            else
            {
                lb_Telephone.Text = user.UsersContact.Phone.ToString();
            }
            if (string.IsNullOrEmpty(user.UsersContact.MobilePhone.ToString()))
            {
                pn_MobilePhone.Visible = false;
            }
            else
            {
                lb_MobilePhone.Text = user.UsersContact.MobilePhone.ToString();
            }
            if (string.IsNullOrEmpty(user.UsersContact.Fax.ToString()))
            {
                pn_fax.Visible = false;
            }
            else
            {
                lb_fax.Text = user.UsersContact.Fax.ToString();
            }
        }
    }

    /// <summary>
    /// 不显示用户联系方式信息--当用户还未填写时
    /// </summary>
    private void UnVisibleUserContactlInfo()
    {
        pn_QQ.Visible = false;
        pn_MSN.Visible = false;
        pn_Telephone.Visible = false;
        pn_MobilePhone.Visible = false;
        pn_fax.Visible = false;
    }
    #endregion

    private void InitItems_SelfPage()
    {
        pn_UnderImageItem_FriendPage.Visible = false;
        pn_UnderImageItem_SelfPage.Visible = true;
        hl_UserImageEdit.NavigateUrl = "~/Home/Profile.aspx?show=userimage";
        hl_UserProfileEdit.NavigateUrl = "~/Home/Profile.aspx?show=profile";
        hl_UserAccountManagement.NavigateUrl = "~/Home/Profile.aspx?show=account";
        hl_Privacy.NavigateUrl = "~/Home/Profile.aspx?show=privacy";
    }


    private void InitItems_FriendPage()
    {
        pn_UnderImageItem_SelfPage.Visible = false;
        pn_UnderImageItem_FriendPage.Visible = true;
        hl_SendMessage.NavigateUrl = "~/Home/SendShortMessage.aspx?uids=" + ApiHelper.Instance.GetRequestUserId();
        if (FriendRules.Instance.IsFriend(UserRules.Instance.GetCurrentUserId(), ApiHelper.Instance.GetRequestUserId())
            || UserRules.Instance.GetCurrentUserId() == ApiHelper.Instance.GetRequestUserId())
        {
            pnlAddFriend.Visible = false;
        }
    }


    /// <summary>
    /// 返回性别名称
    /// </summary>
    /// <param name="gender"></param>
    /// <returns></returns>
    public string GetGenderName(bool gender)
    {
        if (gender == true)
        {
            return "女";
        }
        else
        {
            return "男";
        }
    }
}
